package com.easylinkin.linkappapi.lobar.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.dto.UserBlacklistDTO;
import com.easylinkin.linkappapi.lobar.entity.UserBlacklist;
import com.easylinkin.linkappapi.lobar.entity.emp.EmpUserBase;
import com.easylinkin.linkappapi.lobar.entity.emp.EmpUserBaseDTO;
import com.easylinkin.linkappapi.lobar.mapper.EmpUserBaseMapper;
import com.easylinkin.linkappapi.lobar.service.UserBlacklistService;
import com.easylinkin.linkappapi.lobar.service.UserProjectService;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 花名册黑名单管理
 */
@RestController
@RequestMapping("userBlacklist")
@Api(tags = "花名册黑名单管理")
public class UserBlacklistController {
  
  @Autowired
  private UserBlacklistService userBlacklistService;

  @Autowired
  private EmpUserBaseMapper empUserBaseMapper;

  /**
   * @Description: 增加花名册黑名单
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加花名册黑名单")
  @CommonOperateLogAnnotate(module = LogConstant.LogModule.LABOR_MANAGEMENT, desc = "")
  public RestMessage insert(@RequestBody UserBlacklist userBlacklist){
    //参数校验
    //记录日志
   /* if(userBlacklist != null) {
      if (!Integer.valueOf(2).equals(userBlacklist.getEnterpriseSourceType())) {
        //项目端日志
        EmpUserBase empUserBase = empUserBaseMapper.selectById(userBlacklist.getUserId());
        if (empUserBase != null) {
          LogHelper.setContent(LogConstant.LogOperateType.LABOL_ROSTER_ADD_BLACK, empUserBase.getName(), empUserBase.getCard());
        }
      }
    }*/
    //参数验证
    Map<String, Object> map = userBlacklistService.insert(userBlacklist);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 审批黑名单
   */
  @PostMapping(value = "approve")
  @ApiOperation("审批黑名单")
  @CommonOperateLogAnnotate(module = LogConstant.LogModule.LABOR_MANAGEMENT, desc = "审批黑名单")
  public RestMessage approve(@RequestBody UserBlacklist userBlacklist){
    //参数验证
    Map<String, Object> map = userBlacklistService.approve(userBlacklist);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * @Description: 移出黑名单
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("移出黑名单")
  public RestMessage delOne(@RequestBody UserBlacklist userBlacklist){
    Assert.notNull(userBlacklist.getId(),"id不能为空");
    Map<String, Object> map = userBlacklistService.delOne(userBlacklist, 2);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * @Description: 删除黑名单列表(企业级操作)
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping("delBatch")
  @ApiOperation("删除黑名单列表")
  public RestMessage delBatch(@RequestBody UserBlacklist userBlacklist){
    Assert.notEmpty(userBlacklist.getIds(),"id不能为空");
    userBlacklistService.delBatch(userBlacklist);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑花名册黑名单
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑花名册黑名单")
  public RestMessage updateById(@RequestBody UserBlacklist userBlacklist){
    Assert.notNull(userBlacklist.getId(),"id不能为空");
    userBlacklistService.updateOne(userBlacklist);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询花名册黑名单详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询花名册黑名单详情")
  public RestMessage findById(@PathVariable("id")Long id) {
    Assert.notNull(id,"id不能为空");
    UserBlacklist userBlacklist = userBlacklistService.getById(id);
    if (StringUtils.isNotBlank(userBlacklist.getUserId())) {
      EmpUserBase empUserBase = empUserBaseMapper.selectById(userBlacklist.getUserId());
      if (empUserBase != null){
        EmpUserBaseDTO empUserBaseDTO = new EmpUserBaseDTO();
        BeanUtils.copyProperties(empUserBase, empUserBaseDTO);
        empUserBaseDTO.setAge(empUserBaseDTO.getAge());
        userBlacklist.setBaseUser(empUserBaseDTO);
      }
    }
    return RestBuilders.successBuilder().data(userBlacklist).build();
  }

  /**
   * @Description: 根据条件，分页(不分页)查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<UserBlacklistDTO> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    requestModel.getCustomQueryParams().setEnterpriseSourceType(1);
    IPage<UserBlacklistDTO> record =  userBlacklistService.queryListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }
}

